package com.biz.primus.ms.commodity.service.backend;

import com.biz.primus.base.enums.vendor.VendorType;
import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.base.vo.commodity.ProductIdxIdentityVo;
import com.biz.primus.commodity.exception.CommodityExceptionType;
import com.biz.primus.commodity.vo.backend.DepotProductDeleteItemEventVo;
import com.biz.primus.commodity.vo.backend.DepotProductModifyEventVo;
import com.biz.primus.common.utils.JsonUtil;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.commodity.dao.po.DepotProduct;
import com.biz.primus.ms.commodity.dao.po.Product;
import com.biz.primus.ms.commodity.dao.redis.DepotProductRedisDao;
import com.biz.primus.ms.commodity.dao.repository.product.DepotProductRepository;
import com.biz.primus.ms.commodity.dao.repository.product.ProductRepository;
import com.biz.primus.ms.commodity.dao.ro.DepotProductRo;
import com.biz.primus.ms.commodity.trans.DepotProduct2DepotProductRo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by jiangnan on 2017/6/23.
 */
@Slf4j
@Service
public class DepotProductService extends AbstractBaseService {

    @Autowired
    private DepotProductRepository depotProductRepository;

    @Autowired
    private DepotProductRedisDao depotProductRedisDao;

    @Autowired
    private ProductRepository productRepository;

//    todo:nan
//    @Autowired
//    private MessageService messageService;

    @Transactional
    public void saveOrUpdateDepotProduct(DepotProductModifyEventVo reqVo) {
        DepotProduct depotProduct = depotProductRepository.findByDepotIdAndProductId(reqVo.getDepotId(), reqVo.getProductId());
        if (depotProduct == null) {
            depotProduct = new DepotProduct();
            depotProduct.setId(idService.getNextId());
        }
        depotProduct.setDepotCode(reqVo.getDepotCode());
        depotProduct.setDepotId(reqVo.getDepotId());
        Product product = productRepository.findOne(reqVo.getProductId());
        AssertUtils.notNull(product, CommodityExceptionType.PRODUCT_NOT_FOUND);
        depotProduct.setProduct(product);
        depotProduct.setDepotProductOrder(reqVo.getDepotProductSort());
        depotProduct.setProvinceId(reqVo.getProvinceId());
        depotProductRepository.save(depotProduct);
        final DepotProductRo depotProductRo = new DepotProduct2DepotProductRo().apply(depotProduct);
        delayer.executeAfterTransactionCommit(() -> {
            depotProductRedisDao.save(depotProductRo);
        });

        ProductIdxIdentityVo productIdxIdentityVo = new ProductIdxIdentityVo();
        productIdxIdentityVo.setProductCode(product.getProductCode());
        productIdxIdentityVo.setProductType(VendorType.TYPE_B.getValue());
        productIdxIdentityVo.setWillBeDeleted(Boolean.TRUE);
        productIdxIdentityVo.setDepotCode(reqVo.getDepotCode());
//        productIdxIdentityVo.setCenterDepotCode(reqVo.getWarehouseDepotCode());
//        productIdxIdentityVo.setDepotProductOrder(reqVo.getDepotProductSort());
        log.info("触发商品增量索引 reqVo = {}", productIdxIdentityVo);
//        messageService.sendMessage(BizBaseQueue.MQ_SEARCH_PRODUCT_QUEUE, SimpleBizMessage.newMessage(productIdxIdentityVo));
    }

    @Transactional
    public void delete(String warehouseCode, DepotProductDeleteItemEventVo depotProductDeleteItemEventVo) {
        List<DepotProduct> depotProducts = depotProductRepository.findByProductIdAndProvinceId(depotProductDeleteItemEventVo.getProductId(), depotProductDeleteItemEventVo.getProvinceId());
        log.info(JsonUtil.obj2Json(depotProductDeleteItemEventVo));
        log.info("要删除的商品排序数量{}", depotProducts.size());
        for (DepotProduct depotProduct : depotProducts) {
            depotProduct.setDepotProductOrder(102);
        }
        depotProductRepository.save(depotProducts);
        final List<DepotProductRo> depotProductRos = Lists.transform(depotProducts, new DepotProduct2DepotProductRo());
        this.delayer.executeAfterTransactionCommit(() -> {
            depotProductRedisDao.deleteAll(depotProductRos);
        });
        for (DepotProduct depotProduct : depotProducts) {
            ProductIdxIdentityVo productIdxIdentityVo = new ProductIdxIdentityVo();
            productIdxIdentityVo.setProductCode(depotProduct.getProduct().getProductCode());
            productIdxIdentityVo.setProductType(VendorType.TYPE_B.getValue());
            productIdxIdentityVo.setWillBeDeleted(Boolean.TRUE);
            productIdxIdentityVo.setDepotCode(depotProduct.getDepotCode());
//            productIdxIdentityVo.setCenterDepotCode(warehouseCode);
            log.info("触发商品增量索引 reqVo = {}", productIdxIdentityVo);
//            messageService.sendMessage(BizBaseQueue.MQ_SEARCH_PRODUCT_QUEUE, SimpleBizMessage.newMessage(productIdxIdentityVo));

        }
    }


}
